package com.smartian.dnshosts;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class DnsHostList {

    private Map<String, String> hosts = new HashMap<>();

//    {
//        addHost("www.baidu.com", "127.0.0.1");
//        addHost("www.taobao.com", "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b");
//    }

    public void addHost(String domain, String ip) {
        if (TextUtils.isEmpty(domain) || TextUtils.isEmpty(ip)) {
            throw new IllegalArgumentException("empty domain or ip can't for hooking");
        }
        boolean domainIsIpAddress = InetAddressUtils.isMatcherIpV4OrIpV6(domain);
        if (domainIsIpAddress) {
            throw new IllegalArgumentException("error domain format,it is ip address?");
        }
        boolean ipIsIpAddress = InetAddressUtils.isMatcherIpV4OrIpV6(ip);
        if (!ipIsIpAddress) {
            throw new IllegalArgumentException("error ip format,can't for use");
        }
        hosts.put(domain, ip);
    }

    public boolean isEmpty() {
        return hosts == null || hosts.isEmpty();
    }

    public String lookup(String hostName, int netId) {
        String ip = hosts.get(hostName);
        return ip;
    }

    public void addDnsHosts(Map<String, String> dnsHostMap) {
        if (dnsHostMap == null || dnsHostMap.isEmpty()) return;
        for (Map.Entry<String, String> entry : dnsHostMap.entrySet()) {
            addHost(entry.getKey(), entry.getValue());
        }
    }
}
